function formatTime(date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()


    return [hour, minute, second].map(formatNumber).join(':')
}

function formatTime1(date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    return [year, month, day].map(formatNumber).join('/')
}

function formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
}
// 获取当前日期
function getNowTime() {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    if (month < 10) {
        month = '0' + month;
    }
    if (day < 10) {
        day = '0' + day;
    }
    var nowDate = year + "-" + month + "-" + day;
    return nowDate;
}

// 获取当前日期
function getNowDatas() {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();

    if (month < 10) {
        month = '0' + month;
    }
    if (day < 10) {
        day = '0' + day;
    }
    var nowDate = {
        'year': year,
        'month': month,
        'day': day
    }
    return nowDate;
}
/**
 * 
 * @param {*} data 需要传入的数据，当不需要传入数据的时候，传入空字符串
 * @param {*} path api路径，已经默认增加了/api
 * @param {*} method 访问方法，get,post等
 * @param {*} cb 回调函数
 */

// api网站的网址
// var appid = 'wx06904fb95d7fefa6';
// var version = 0;
var purl = 'https://bwgxcxapitest.taoomall.com';
var token = wx.getStorageSync('token');
var account = wx.getStorageSync('account')
    // 请求接口
function myrequest(data, path, method, cb) {
    wx.request({
        url: purl + path,
        header: { 'Content-Type': 'application/json', 'ng-params-one': wx.getStorageSync('account'), 'ng-params-two': wx.getStorageSync('token') },
        data: data,
        method: method,
        success: function(res) {
            if (res.data.res == false) {
                // 校验失败
                // console.log(222)
                // wx.showToast({
                //     title: '您尚未登陆请先登陆',
                //     icon: 'loading',
                //     duration: 2000
                // })
                // setTimeout(function() {
                //     wx.navigateTo({
                //         url: '../logins/logins'
                //     })
                // }, 1000)
            }
            if (typeof cb == "function") {
                cb(res.data);
            }
        },

        // 登入失败
        fail: function() {
            wx.showModal({
                title: '警告',
                content: '网络异常！',
                success: function(res) {
                    if (res.confirm) {
                        // console.log('用户点击确定')
                    }
                }
            })
        },
    })
}

function checkLogin() {
    if (!wx.getStorageSync('token')) {
        wx.showToast({
            title: '您尚未登陆请先登陆',
            icon: 'loading',
            duration: 2000
        })
        setTimeout(function() {
            wx.navigateTo({
                url: '../logins/logins'
            })
        }, 1000)
    } else {
        console.log("login")
    }
}
module.exports = {
    formatTime: formatTime,
    myrequest: myrequest,
    formatTime1: formatTime1,
    checkLogin: checkLogin,
    getNowTime: getNowTime,
    getNowDatas: getNowDatas

}